﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace ERPBackend.Migrations
{
    /// <inheritdoc />
    public partial class InventoryMigration : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_purchase_receipt_supplier_supplier_id",
                table: "purchase_receipt");

            migrationBuilder.AddColumn<string>(
                name: "unit",
                table: "material",
                type: "nvarchar(50)",
                maxLength: 50,
                nullable: false,
                defaultValue: "");

            migrationBuilder.CreateTable(
                name: "inventory",
                columns: table => new
                {
                    id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    material_id = table.Column<int>(type: "int", nullable: false),
                    warehouse_id = table.Column<int>(type: "int", nullable: false),
                    batch_number = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true),
                    current_stock = table.Column<decimal>(type: "decimal(18,4)", nullable: false),
                    safety_stock = table.Column<decimal>(type: "decimal(18,4)", nullable: false),
                    max_stock = table.Column<decimal>(type: "decimal(18,4)", nullable: false),
                    unit = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    stock_amount = table.Column<decimal>(type: "decimal(18,4)", nullable: false),
                    last_stock_in_date = table.Column<DateTime>(type: "datetime2", nullable: true),
                    last_stock_out_date = table.Column<DateTime>(type: "datetime2", nullable: true),
                    status = table.Column<byte>(type: "tinyint", nullable: false),
                    created_by = table.Column<int>(type: "int", nullable: false),
                    created_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    updated_by = table.Column<int>(type: "int", nullable: false),
                    updated_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    is_deleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_inventory", x => x.id);
                    table.ForeignKey(
                        name: "FK_inventory_material_material_id",
                        column: x => x.material_id,
                        principalTable: "material",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_inventory_warehouse_warehouse_id",
                        column: x => x.warehouse_id,
                        principalTable: "warehouse",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "inventory_transaction",
                columns: table => new
                {
                    id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    transaction_code = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    transaction_type = table.Column<byte>(type: "tinyint", nullable: false),
                    transaction_date = table.Column<DateTime>(type: "datetime2", nullable: false),
                    material_id = table.Column<int>(type: "int", nullable: false),
                    warehouse_id = table.Column<int>(type: "int", nullable: false),
                    batch_number = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: true),
                    transaction_quantity = table.Column<decimal>(type: "decimal(18,4)", nullable: false),
                    before_stock = table.Column<decimal>(type: "decimal(18,4)", nullable: false),
                    after_stock = table.Column<decimal>(type: "decimal(18,4)", nullable: false),
                    operator_id = table.Column<int>(type: "int", nullable: false),
                    remark = table.Column<string>(type: "nvarchar(500)", maxLength: 500, nullable: true),
                    created_by = table.Column<int>(type: "int", nullable: false),
                    created_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    updated_by = table.Column<int>(type: "int", nullable: false),
                    updated_time = table.Column<DateTime>(type: "datetime2", nullable: false),
                    is_deleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_inventory_transaction", x => x.id);
                    table.ForeignKey(
                        name: "FK_inventory_transaction_material_material_id",
                        column: x => x.material_id,
                        principalTable: "material",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_inventory_transaction_user_operator_id",
                        column: x => x.operator_id,
                        principalTable: "user",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_inventory_transaction_warehouse_warehouse_id",
                        column: x => x.warehouse_id,
                        principalTable: "warehouse",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_inventory_material_id_warehouse_id",
                table: "inventory",
                columns: new[] { "material_id", "warehouse_id" },
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_inventory_warehouse_id",
                table: "inventory",
                column: "warehouse_id");

            migrationBuilder.CreateIndex(
                name: "IX_inventory_transaction_material_id",
                table: "inventory_transaction",
                column: "material_id");

            migrationBuilder.CreateIndex(
                name: "IX_inventory_transaction_operator_id",
                table: "inventory_transaction",
                column: "operator_id");

            migrationBuilder.CreateIndex(
                name: "IX_inventory_transaction_transaction_code",
                table: "inventory_transaction",
                column: "transaction_code",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_inventory_transaction_warehouse_id",
                table: "inventory_transaction",
                column: "warehouse_id");

            migrationBuilder.AddForeignKey(
                name: "FK_purchase_receipt_supplier_supplier_id",
                table: "purchase_receipt",
                column: "supplier_id",
                principalTable: "supplier",
                principalColumn: "id");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_purchase_receipt_supplier_supplier_id",
                table: "purchase_receipt");

            migrationBuilder.DropTable(
                name: "inventory");

            migrationBuilder.DropTable(
                name: "inventory_transaction");

            migrationBuilder.DropColumn(
                name: "unit",
                table: "material");

            migrationBuilder.AddForeignKey(
                name: "FK_purchase_receipt_supplier_supplier_id",
                table: "purchase_receipt",
                column: "supplier_id",
                principalTable: "supplier",
                principalColumn: "id",
                onDelete: ReferentialAction.Cascade);
        }
    }
}
